angular.module('contact', ['head-toast'])

.controller 'contactCtrl', ($scope, $state, $http, $ionicHistory, userData, $ionicPopup,mallInfo,showToast) ->
    _.extend $scope,
        user:
            phone: undefined
            address: undefined
        selectData:
            province: undefined
            cityData: undefined
            city:undefined
            currentProvince:''
            currentCity:''
        formValidation: ->
            Mobile = $scope.user.phone.match(/^(13[0-9]|14[0-9]|15[0-9]|18[0-9])\d{8}$/)==null
            phone = $scope.user.phone.match(/^(\\d{3,4}-)\\d{7,8}$/)==null

            if Mobile and phone
                showToast '联系电话格式不正确'
                return false
            else
                return true
        getProvinceAndCity: (provinceid,cityid)->
            $http.get('/seller/', params: {action: 'areadata'})
            .success (resp) ->
                if resp?.status == '0'
                    $scope.selectData.province = resp.body[0].province
                    $scope.selectData.cityData = resp.body[1].city
                    $scope.selectData.currentProvince = _.find $scope.selectData.province, (item)->
                        item.provinceid == provinceid
                    $scope.getCity(cityid)
                else
                    showToast resp.error
            .error ->
                showToast '服务器处理失败'
        goBack: ->
            $ionicHistory.goBack()
        getCity: (cityid)->
            $scope.selectData.city = _.filter $scope.selectData.cityData, (item)->
                item.provinceid == $scope.selectData.currentProvince.provinceid
            $scope.selectData.currentCity = _.find $scope.selectData.city, (item)->
                item.cityid == cityid
        goSave: ->
            if $scope.formValidation()
                $http.post('/utf-8data/', {
                            action: 'editshopinfo',
                            userid: userData.getToken(),
                            phone: $scope.user.phone,
                            address: $scope.user.address,
                            province: $scope.selectData.currentProvince.provinceid,
                            city: $scope.selectData.currentCity.cityid
                        }
                )
                .success (resp) ->
                    if resp?.status == '0'
                        $ionicHistory.goBack()
                    else
                        showToast resp.error
                .error ->
                    showToast '服务器处理失败'
        clear: (i) ->
            if i == '4'
                $scope.user.phone = ''
            else if i == '5'
                $scope.user.address = ''
    $scope.user = mallInfo.mall
    $scope.getProvinceAndCity($scope.user.province,$scope.user.city);


